package com.exrobot.module.rbt.controller.admin.canteeninfo;

import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.canteeninfo.vo.*;
import com.exrobot.module.rbt.dal.dataobject.canteeninfo.CanteenInfoDO;
import com.exrobot.module.rbt.service.canteeninfo.CanteenInfoService;

@Tag(name = "管理后台 - 食堂信息")
@RestController
@RequestMapping("/rbt/canteen-info")
@Validated
public class CanteenInfoController {

    @Resource
    private CanteenInfoService canteenInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建食堂信息")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-info:create')")
    public CommonResult<Long> createCanteenInfo(@Valid @RequestBody CanteenInfoSaveReqVO createReqVO) {
        return success(canteenInfoService.createCanteenInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新食堂信息")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-info:update')")
    public CommonResult<Boolean> updateCanteenInfo(@Valid @RequestBody CanteenInfoSaveReqVO updateReqVO) {
        canteenInfoService.updateCanteenInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除食堂信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:canteen-info:delete')")
    public CommonResult<Boolean> deleteCanteenInfo(@RequestParam("id") Long id) {
        canteenInfoService.deleteCanteenInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除食堂信息")
                @PreAuthorize("@ss.hasPermission('rbt:canteen-info:delete')")
    public CommonResult<Boolean> deleteCanteenInfoList(@RequestParam("ids") List<Long> ids) {
        canteenInfoService.deleteCanteenInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得食堂信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-info:query')")
    public CommonResult<CanteenInfoRespVO> getCanteenInfo(@RequestParam("id") Long id) {
        CanteenInfoDO canteenInfo = canteenInfoService.getCanteenInfo(id);
        return success(BeanUtils.toBean(canteenInfo, CanteenInfoRespVO.class));
    }
    @GetMapping("/simple-list")
    @Operation(summary = "获得食堂列表")
    @PermitAll
    public CommonResult<List<CanteenInfoRespVO>> getSimpleList() {
        CanteenInfoPageReqVO pageReqVO = new CanteenInfoPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<CanteenInfoDO> pageResult = canteenInfoService.getCanteenInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult.getList(), CanteenInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得食堂信息分页")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-info:query')")
    public CommonResult<PageResult<CanteenInfoRespVO>> getCanteenInfoPage(@Valid CanteenInfoPageReqVO pageReqVO) {
        PageResult<CanteenInfoDO> pageResult = canteenInfoService.getCanteenInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CanteenInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出食堂信息 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:canteen-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCanteenInfoExcel(@Valid CanteenInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CanteenInfoDO> list = canteenInfoService.getCanteenInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "食堂信息.xls", "数据", CanteenInfoRespVO.class,
                        BeanUtils.toBean(list, CanteenInfoRespVO.class));
    }

}